/*
 * @Author: 田斌 tianbin0912@163.com
 * @Date: 2023-07-20 13:14:59
 * @LastEditors: 田斌 tianbin0912@163.com
 * @LastEditTime: 2023-08-10 17:50:18
 * @FilePath: \jQueryProject\rollup.config.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// rollup.config.js
// rollup.config.js
import fs from 'fs';
import path from 'path';
import rollupTypescript from 'rollup-plugin-typescript2';
import ts from "rollup-plugin-typescript2"// 解析ts
import resolvePlugin from "@rollup/plugin-node-resolve" // 解析第三方库
import url from '@rollup/plugin-url';
// import svg from 'rollup-plugin-svg'
import image from '@rollup/plugin-image';
import { terser } from 'rollup-plugin-terser';
import postcss from 'rollup-plugin-postcss';
import postCssPxToRem from 'postcss-pxtorem';
import babel from '@rollup/plugin-babel';
const mode = process.env.NODE_ENV;
const size = process.env.SIZE || 20;
console.log(mode, 'mode');
// function getEntryPoints() {
//   const entryPath = 'packages';
//   const entryFiles = fs.readdirSync(entryPath);
//   const entryPoints = {};

//   entryFiles.forEach((file) => {
//     const filePath = path.join(entryPath, file, 'index.ts');
//     if (fs.existsSync(filePath)) {
//       const componentName = file;
//       entryPoints[componentName] = {
//         input: filePath,
//         output: {
//           file: `dist/${mode === 'production' ? 'prop' : 'dev'}/${componentName}/index.js`,
//           format: 'umd',
//           assetFileNames: 'assets/[name][extname]',
//           name: componentName,
//           globals: {
//             componentName: componentName,
//           }
//         },
//       };
//     }
//   });

//   return entryPoints;
// }

// const rollupConfig = Object.entries(getEntryPoints()).map(([componentName, config]) => ({
// 	input: config.input,
// 	output: config.output,
// 	plugins: [
//     rollupTypescript(),
//     image(),
//     url({
//       include: ['**/*.svg', '**/*.png', '**/*.jpg', '**/*.gif'],
//       limit: 0,
//       emitFiles: true,
//     }),
// 	  postcss({
// 		extract: `index.css`, // 确保样式文件被独立打包成CSS
// 		extensions: ['.less'],
// 		minimize: true,
// 		use: {
// 		  less: { javascriptEnabled: true },
// 		},
// 	  }),
// 	],
//   }));
  
//   export default rollupConfig;

let packagesDir = path.resolve(__dirname, 'packages')
// 2.1 获取需要进行打包的包
let packageDir = path.resolve(packagesDir, process.env.TARGET)
// 2.2 获取每一个包的项目配置
const resolve = p => path.resolve(packageDir, p)
const pkg = require(resolve('package.json'))
const packageOptions = pkg.buildOptions || {} 
// 3 创建一个映射表，
const name = path.basename(packageDir)
const outputOptiosn = {
  // "esm-bundler": {
  //   file: resolve(`dist/${name}.esm-bundler.js`),
  //   format: 'es'
  // },
  // "cjs": {
  //   file: resolve(`dist/${name}.cjs.js`),
  //   format: 'cjs'
  // },
  // "global": {
  //   file: resolve(`dist/${name}.global.js`),
  //   format: 'iife'
  // }
  "umd": {
    file: resolve(`dist/${mode === 'production' ? 'prop' : 'dev'}/${name}.umd.js`),
    format: 'umd',
    assetFileNames: 'assets/[name][extname]',
    name: name,
    globals: {
      name: name,
    }
  }
}
const options = pkg.buildOptions
// 导出配置
function createConfig(format, output) {
  // 进行打包
  output.sourcemap = mode === 'development'

  // 生成rollup配置
  console.log(output, 'output');
  return {
    input: resolve('src/index.ts'), // 导入
    output,
    plugins: [
      ts({
        tsconfig: path.resolve(__dirname, 'tsconfig.json')
      }),
      
      resolvePlugin(),
      babel({
        exclude: 'node_modules/**',
        babelHelpers: 'bundled',
        extensions: ['.js', '.jsx', '.ts', '.tsx'],
      }),
      image(),
      url({
        include: ['**/*.svg', '**/*.png', '**/*.jpg', '**/*.gif'],
        limit: 0,
        emitFiles: true,
      }),
      postcss({
        
      extract: `index.css`, // 确保样式文件被独立打包成CSS
      extensions: ['.less'],
      minimize: true,
      use: {
        less: { javascriptEnabled: true },
      },
      plugins: [
        postCssPxToRem({
          rootValue: size,
          propList: ['*'],
          exclude: /node_modules/i,
        }),
      ],
      }),
      mode === 'production' ? terser() : null 
    ]
  }
}
export default options.formats.map(format => createConfig(format, outputOptiosn[format]))